package com.wulang.order.controller;


import com.wulang.order.model.OrderInfo;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;
import java.util.UUID;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    RabbitTemplate template;

    //发送字符串
    @RequestMapping("/create")
    @ResponseBody
    public String createOrder() {
        //这里相关的业务代码省略
        String orderId = UUID.randomUUID().toString();
        template.convertAndSend("", "order.queue", "下单成功, 订单id:" + orderId);
        return "下单成功";
    }

    //发送对象
    @RequestMapping("/create2")
    @ResponseBody
    public String createOrder2() {
        String orderId = UUID.randomUUID().toString();
        //发送这个订单对象到消息队列当中
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setOrderId(orderId);
        orderInfo.setName("商品: " + new Random().nextInt(100));
        template.convertAndSend("", "order.queue", orderInfo);
        return "下单成功";
    }
}
